#include <CGalaxyComponentStars.h>

double CGalaxyComponentStars::calculateIntensity(double I, CVector& p, double iscale) {
    CVector P;
    double z;
    double rad = getRadius(p, P, z);
    //height = 1;
    double winding = getWinding(rad)*componentParams.winding;
    z = getHeightModulation(z);

    double shift = componentParams.arm*111.12523;


    if (z>0.001) 
      {
      
	double armVal = 1.0;
	if (componentParams.arm!=0)
	  armVal =  calculateArmValue(rad, P, componentParams.arm);

	if (armVal<0.01)
	  return I;
	
	double intensity = getRadialIntensity(rad);

	CVector r = p;
	//winding = 0;
	RX.RotateXZ(winding);
	r = RX.Mul(r);


	double perlinnoise = 0;
	//10
	for (int ii=1;ii<20;ii++) {
	  double k= (ii*3 + 8)*componentParams.scale;
	  double pp = abs(perlin->Get( (r.x + 0.1 + params->randShiftX*51.0)*k, (r.y+ 0.331 + shift*0.5+params->randShiftY*39.1)*k, (r.z + 0.05 +shift + params->randShiftY*(41.7))*k ));///pow(ii,0.25);
	  perlinnoise+=pp;
	}
	perlinnoise*=1;
	perlinnoise/=1.75;
	perlinnoise-=0.1;
	if (perlinnoise<0) perlinnoise = 0;
	double val = 10.0*pow(perlinnoise,10.0);
	//if (val>100) val = 100;
	//cout << val << endl;
	
	//	double scale = CMath::Minmax(pow(CMath::Minmax(rad-0.0f,0,1), params->bulgeDust), 0.0, 1);
	double scale = 1.0;

	double bulgeScale = CMath::SmoothStep(0, 2.0*params->bulgeDust, rad);

	//I+=val;

	I+=strength*armVal*val*intensity*z*scale*pow(params->diskStarModifier,2)*params->intensityScale*bulgeScale*iscale;

		//I+=strength*armVal*val*intensity*z*scale*params->intensityScale*bulgeScale*iscale;

		//I+=strength*armVal*val*intensity*z*scale*params->intensityScale*bulgeScale;
	//I+=strength*armVal*val*intensity*z*scale;
      
    }
    return I;
  }
